﻿/* ***********************************************************************************************
 * Oak_Waypoint
 * Version: 1.0
 * Author:  BRyan Solero  
 * Email:  arindil@gmail.com
 * Copyright 2012.  All Rights Reserved.
 * 
 * ************************************************************************************************/

using System;


using Microsoft.Xna.Framework;
using OakEngine.AI.Steering;

namespace OakEngine.AI.Waypoint
{
    public class Oak_Waypoint
    {
        public Vector2 m_Position;
        public float m_fRotation;


        public Oak_Waypoint()
        {
            this.m_fRotation = 0;
        }

        public Oak_Waypoint(Vector2 pos)
        {
            this.m_Position = pos;
            this.m_fRotation = 0;
        }

        public Oak_Waypoint(Vector2 pos, float angle)
        {
            this.m_Position = pos;
            this.m_fRotation = angle;
        }

        public Oak_Waypoint(float x, float y, float angle)
        {
            this.m_Position = new Vector2(x,y);
            this.m_fRotation = angle;
        }

        public void ZeroOut()
        {
            this.m_Position = Vector2.Zero;
            this.m_fRotation = 0.0f;
        }

        // Operators
        public static bool operator ==(Oak_Waypoint lhs, Oak_Waypoint rhs)
        {
            return (lhs.m_Position == rhs.m_Position && lhs.m_fRotation == rhs.m_fRotation);
        }

        public static bool operator !=(Oak_Waypoint lhs, Oak_Waypoint rhs)
        {
            return (lhs.m_Position != rhs.m_Position || lhs.m_fRotation != rhs.m_fRotation);
        }

        void SetUpdate( Vector2 vel,  float angle, float duration)
        {
            m_Position += vel*duration;
        }

        public void Update(Oak_Steering steer, float duration)
        {
            SetUpdate(steer.ForwardVector,steer.Rotation,duration);
        }

        public void SetRotationFromVelocity(Vector2 vel)
        {
            // If we dont have a direction to go to then just dont move
            float sqM = vel.X*vel.X + vel.Y*vel.Y;
            if (sqM > 0)
            {
                m_fRotation =(float) Math.Atan2(vel.Y, vel.X);
            }
        }

        public Vector2 GetRotationAsVelocity()
        {
            return new Vector2((float)Math.Sin(m_fRotation),(float)Math.Cos(m_fRotation));
        }

    }
}
